<template>
	<view class="parent-view">
		<view class="head-view">
			<image class="head-image" :src="headImage"></image>
		</view>
		<view class="riqi-name-view">
			<input class="riqi-name-input" placeholder="请输入日期名称" :value="title" @input="inputValue"/>
			<text class="riqi-name-txt" @click="nameClick">常用名称</text>
		</view>
		<text class="riqi-view" @click="timeSelect">{{riqi}}</text>
		<text class="btn" v-if="id==0" @click="addDinary">确认添加</text>
		<text class="btn" v-else @click="editDinary">确认编辑</text>
		<choseTime :isShow="isChoseTimeShow" @returnDate="timeReturn"></choseTime>
	</view>
</template>

<script>
	import request from '../index/request.js';
	import choseTime from "@/components/chose-time/time-chose.vue";
	export default {
		components: {
			choseTime
		},
		data() {
			return {
				riqi:"请选择日期",
				title:"",
				headImage:"https://c.shiqin.wang/assets/img/logo2.png",
				isChoseTimeShow:false,
				titleList:[],
				id:0,
				lunar:"",
			}
		},
		methods: {
			onLoad:function(options){
				this.appUtil.saveAppInviteId(options)
				if(options.data){
					console.log(options.data)
					var data=JSON.parse(options.data)
					if(data){
						this.riqi=data.times
						this.title=data.title
						this.headImage=data.avatar
						this.id=data.id
						this.lunar=data.lunar
					}
				}
				this.getLeavemod4()
			},
			onShow:function(){
				this.share.url='/pages/tabBar/home/index?appInviteId='+this.appUtil.getAppInviteId()
				this.share.shareType=1
			},
			inputValue:function(event){
				this.title=event.detail.value
			},
			//获取数据
			getLeavemod4:function(){
				var that=this
				request.post("api/common/get_leavemod4",{
				}).then(data=>{
					for (var i = 0; i < data.length; i++) {
						for (var j = 0; j < data[i].texts.length; j++) {
							that.titleList.push(data[i].texts[j])
						}
					}
				})
			},
			editDinary:function(){
				if(!this.title){
					uni.showToast({
						icon:"none",
						title:"请填写纪念日标题"
					})
					return
				}
				if(this.riqi==='请选择日期'){
					uni.showToast({
						icon:"none",
						title:"请选择日期"
					})
					return
				}
				var that=this
				request.post("api/user/SetUserCommemorationRiV3",{
					title:this.title,
					riqi:this.riqi,
					lunar:this.lunar,
					id:this.id,
					avatar_url:""
				}).then(data=>{
					uni.navigateBack()
				})
			},
			addDinary:function(){
				if(!this.title){
					uni.showToast({
						icon:"none",
						title:"请填写纪念日标题"
					})
					return
				}
				if(this.riqi==='请选择日期'){
					uni.showToast({
						icon:"none",
						title:"请选择日期"
					})
					return
				}
				var that=this
				request.post("api/user/AddUserCommemorationRiV3",{
					title:this.title,
					riqi:this.riqi,
					lunar:"0",
					avatar_url:""
				}).then(data=>{
					uni.navigateBack()
				})
			},
			timeSelect:function(){
				this.isChoseTimeShow=true
			},
			timeReturn:function(e){
				this.isChoseTimeShow=e.isclose
				if(e.startTime){
					this.riqi=e.startTime
				}
			},
			nameClick:function(){
				var that=this
				uni.showActionSheet({
					itemList: this.titleList,
					success (res) {
						that.title=that.titleList[res.tapIndex]
					},
					fail (res) {
						
					}
				})
			}
		}
	}
</script>

<style>
	@import "index.css";
</style>
